package main

import (
	"fmt"
	"github.com/shirou/gopsutil/disk"
	"strings"
)

func main() {
	diskInt := 0
	var oldInfo []disk.PartitionStat
	var pathMap = map[string]string{}
	for {
		info, _ := disk.Partitions(true) //所有分区
		if len(oldInfo) == 0 {
			oldInfo = info
		}
		if len(info) != diskInt {
			diskInt = len(info)
			changeSlice := make([]string, 0)
			pathMapTemp := map[string]string{}
			for _, obj := range info {
				pathMapTemp[obj.Device] = obj.Mountpoint
				if pathMap[obj.Device] == "" || len(pathMap) == 0 {
					//changeMap[obj.Device] = obj.Mountpoint
					changeSlice = append(changeSlice, obj.Mountpoint)
				}
			}
			pathMap = pathMapTemp
			fmt.Printf("盘符变化：%s\n", strings.Join(changeSlice, ","))
		}
	}

	//// 创建文件/目录监听器
	//watcher, err := fsnotify.NewWatcher()
	//if err != nil {
	//	log.Fatal(err)
	//}
	//defer watcher.Close()
	//done := make(chan bool)
	//go func() {
	//	for {
	//		select {
	//		case event, ok := <-watcher.Events:
	//			if !ok {
	//				return
	//			}
	//			// 打印监听事件
	//			log.Println("event:", event)
	//		case event, ok := <-watcher.:
	//			if !ok {
	//				return
	//			}
	//			// 打印监听事件
	//			log.Println("event:", event)
	//		case _, ok := <-watcher.Errors:
	//			if !ok {
	//				return
	//			}
	//		}
	//	}
	//}()
	//// 监听系统的根目录目录
	//err = watcher.Add("/")
	//if err != nil {
	//	log.Fatal(err)
	//}
	//<-done
}
